#include "IPNGUtils.hxx"

int main()
{
    int iwidth = 400;
    int iheight = 400;

    cv::Mat pngData = cv::Mat( iwidth, iheight, CV_8UC1, cv::Scalar(0,0,0));

    cv::Mat p00 = pngData(cv::Rect(0,0,iwidth/2,iheight/2));
    cv::Mat p01 = pngData(cv::Rect(iwidth/2,0,iwidth/2,iheight/2));
    cv::Mat p10 = pngData(cv::Rect(0,iheight/2,iwidth/2,iheight/2));
    cv::Mat p11 = pngData(cv::Rect(iwidth/2,iheight/2,iwidth/2,iheight/2));

    p00 = 0;
    p01 = 1;
    p10 = 2;
    p11 = 3;

    cv::Mat palete = cv::Mat(4,1,CV_8UC3,cv::Scalar(255,255,0));
    palete.at<Vec3b>(1,0) = cv::Vec3b(0,0,255);
    palete.at<Vec3b>(2,0) = cv::Vec3b(0,255,0);
    palete.at<Vec3b>(3,0) = cv::Vec3b(255,0,0);

    vector<uchar> fileData = imencode_palette_png(pngData,palete);


    FILE* file = fopen("name.png", "wb");
    fwrite(reinterpret_cast<const char *>(&fileData[0]),fileData.size(),1, file);
    fclose(file);

    FILE* tfile = fopen("Tile_R002_C007.png", "rb");
    //FILE* tfile = fopen("name.png", "rb");
    
    fseek(tfile, 0L, SEEK_END);
    
    int sz = ftell(tfile);

    fseek(tfile, 0L, SEEK_SET);

    char* buffer = new char[sz];

    fread(buffer,sz,1,tfile);

    fclose(tfile);

    printf("File Size:%d\n", sz);

    cv::Mat ipngData;
    cv::Mat ipngPalette;

    imdecode_palette_png( buffer, sz, ipngData, ipngPalette );

    delete [] buffer;

    vector<uchar> resultData = imencode_palette_png(ipngData,ipngPalette);

    FILE* fresult = fopen("Result.png", "wb");
    fwrite(reinterpret_cast<const char *>(&resultData[0]),resultData.size(),1, fresult);
    fclose(fresult);    

    return 0;
}
